package Connectivity;

import java.io.IOException;

import java.sql.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Initialize extends HttpServlet 
{

	private static final long serialVersionUID = 1L;

	ServletConfig context;
	String connurl=null;
  
    Connection con=null;    
	
	
	/**
	 * Constructor of the object.
	 */
	public Initialize() 
	{
		super();
	}

	public void init(ServletConfig config)// throws ServletException 
	{
		//connurl="jdbc:postgresql://localhost:5431/CRM";
		connurl="jdbc:mysql://localhost:3300/crm";
		
		
			try 
			{   
				//Class.forName("org.postgresql.Driver");
				Class.forName("com.mysql.jsbc.Driver");
				con=DriverManager.getConnection(connurl,"CRMUser","root");
				//System.out.println("In database connectivity"+con);
				ServletContext context=config.getServletContext();
				context.setAttribute("dbcon",con);
				//System.out.println("after database connectivity"+con);
				
				
				
			} 
			catch (ClassNotFoundException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (SQLException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch(Exception e1){}
			try
			{super.init();}
			catch(Exception e3){}

		
		// Put your code here
	}
	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() 
	{
		super.destroy(); // Just puts "destroy" string in log
		try 
		{
			con.close();
		} 
		catch (SQLException e) 
		{
			
			e.printStackTrace();
		}
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException 
	{Logger.log("Connected get");
response.sendRedirect("/Genius/form/general/Login.jsp");
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
		.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();*/
}
	
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
			{
		Logger.log("Connected");
		response.sendRedirect("/Genius/form/general/Login.jsp");
/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */


}
